package com.unbank;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.unbank.util.JsoupUtil;

public class Test {

	public static void main(String[] args) {
		String url = "http://news.hexun.com/2014-05-09/164634482.html";
		Document document = JsoupUtil.readUrl(url);
		getNewsContent(document);
	}

	private static void getNewsContent(Document document) {
		Element bodyElements = document.select("body").get(0);
		Element parentElement =getParentContent(bodyElements);
		Element element =getChildContent(parentElement);
		
		
		
//		System.out.println(contentfilter(getContent(bodyElements).toString()));
	}

	private static Element getChildContent(Element parentElement) {
	
		return null;
	}

	private static Element getParentContent(Element sourceElement) {

		// Elements elements = document.select("body").get(0).getAllElements();
		Elements elements = sourceElement.getAllElements();
		Map<Element, Integer> childs = new HashMap<Element, Integer>();

		for (Element element : elements) {
			if (element.childNodeSize() <= 2 && !element.tagName().equals("a")
					&& !element.tagName().equals("script")
					&& !element.tagName().equals("style")
					&& !element.tagName().equals("link")
					&& !element.tagName().equals("iframe")
					&& !element.tagName().equals("input")
					&& !element.tagName().equals("img")
					&& !element.tagName().equals("li")
					&& !element.tagName().equals("form")) {
				if (element.text().length() > 20) {
					Element parentElement = element.parent();
					int textlength = parentElement.text().length();
					childs.put(parentElement, textlength);
				}

			}
		}

		List<Map.Entry<Element, Integer>> childsList = new ArrayList<Map.Entry<Element, Integer>>(
				childs.entrySet());
		// 对Map 进行排序么？
		Collections.sort(childsList,
				new Comparator<Map.Entry<Element, Integer>>() {
					public int compare(Map.Entry<Element, Integer> o1,
							Map.Entry<Element, Integer> o2) {
						return (o2.getValue() - o1.getValue());
					}
				});
		// System.out.println((childsList.get(0).getKey()));
		//
		// // for (Entry<Element, Integer> entry : childsList) {
		// // System.out.println(entry.getKey());
		// // System.out.println(entry.getValue());
		// System.out.println("=======================================");
		// // }
		return childsList.get(0).getKey();
	}

	// 网页过滤器
	private static String contentfilter(String content) {
		content = content.replaceAll("(?is)<!--.*?-->", "");
		content = content.replaceAll("(?is)<textarea.*?>.*?</textarea>", "");
		content = content.replaceAll("(?is)<script.*?>.*?</script>", "");
		content = content.replaceAll("(?is)<style.*?>.*?</style>", "");
		content = content.replaceAll("(?is)<option.*?>.*?</option>", "");
		content = content.replaceAll("(?is)<meta.*?>.*?</meta>", "");
		content = content.replaceAll("(?is)<link.*?>.*?</link>", "");
		content = content.replaceAll("(?is)<noscript.*?>.*?</noscript>", "");
		content = content.replaceAll("(?is)<param.*?>.*?</param>", "");
		content = content.replaceAll("(?is)<iframe.*?>.*?</iframe>", "");
		content = content.replaceAll("&.{2,8};|&#.{2,8};", "");
		content = content.replaceAll("\\?", "");
		content = content.replaceAll(
				"<(?!\\/?(div|img|br|p|table|tbody|tr|th|td|a)).*?>", "");
		content = content.replaceAll(" id=\"\\S[^\"]*\"", "");
		content = content.replaceAll(" class=\"\\S[^\"]*\"", "");
		content = content.replaceAll(" name=\"\\S[^\"]*\"", "");
		content = content.replaceAll(" height=\"\\S[^\"]*\"", "");
		content = content.replaceAll(" width=\"\\S[^\"]*\"", "");
		content = content.replaceAll(" border=\"\\S[^\"]*\"", "");
		// content = content.replaceAll(" alt=\"\\S[^\"]*\"", " ");
		content = content.replaceAll(" style=\"\\S[^\"]*\"", " ");
		content = content.replaceAll(" align=\"\\S[^\"]*\"", " ");
		content = content.replaceAll(" script=\"\\S[^\"]*\"", " ");
		content = content.replaceAll(" language=\"\\S[^\"]*\"", " ");
		content = content.replaceAll(" oldsrc=\"\\S[^\"]*\"", " ");
		content = content.replaceAll(" cellspacing=\"\\S[^\"]*\"", " ");
		content = content.replaceAll(" cellpadding=\"\\S[^\"]*\"", " ");
		content = content.replaceAll(" colspan=\"\\S[^\"]*\"", " ");
		content = content.replaceAll(" bgcolor=\"\\S[^\"]*\"", " ");
		content = content.replaceAll(" valign=\"\\S[^\"]*\"", " ");
		content = content.replaceAll(" _extended=\"\\S[^\"]*\"", " ");
		if (content.length() > 20) {
			return content;
		} else {
			return "";
		}
	}

}
